package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.entity.HistoryFireCock;
import com.yqg.protection.mapper.HistoryFireCockMapper;
import com.yqg.protection.service.IHistoryFireCockService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * HistoryFireCockController 前端控制器
 * @author zxf
 * @since 2025-08-12
 */
@RestController
@RequestMapping("/history-fire-cock")
public class HistoryFireCockController {

    @Resource
    private IHistoryFireCockService historyFireCockService;

    @Resource
    private HistoryFireCockMapper historyFireCockMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody HistoryFireCock historyFireCock) {
        return Result.success(historyFireCockService.saveOrUpdate(historyFireCock));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        HistoryFireCock historyFireCock =historyFireCockService.getById(id);
        return Result.success(historyFireCockService.saveOrUpdate(historyFireCock));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<HistoryFireCock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<HistoryFireCock> list=  historyFireCockService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(historyFireCockService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           Long equipmentInputStoreId,
                           String startTime,
                           String endTime) {
        pageNum = pageNum * pageSize - pageSize;
        List<HistoryFireCock> historyFireCockList = historyFireCockMapper.findPage(pageNum, pageSize, equipmentInputStoreId, startTime, endTime);
        Long count = historyFireCockMapper.findPageCount(equipmentInputStoreId, startTime, endTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", historyFireCockList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     *  根据id查询
     */
    @GetMapping("/getInfo")
    public Result getInfo(@RequestParam Long equipmentInputStoreId,
                          @RequestParam String warnTime) {
        LambdaQueryWrapper<HistoryFireCock> historyFireCockLambdaQueryWrapper = new LambdaQueryWrapper<>();
        historyFireCockLambdaQueryWrapper.eq(HistoryFireCock::getEquipmentInputStoreId, equipmentInputStoreId);
        historyFireCockLambdaQueryWrapper.eq(HistoryFireCock::getSendTime, warnTime);
        return Result.success(historyFireCockService.getOne(historyFireCockLambdaQueryWrapper));
    }
}

